package com.own.component.mybatis.injector.method;

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.own.component.mybatis.em.SqlMethod;
import com.own.component.mybatis.util.SqlScriptUtil;
import org.apache.ibatis.mapping.MappedStatement;

/**
 * UpdateIncreaseOrder
 *
 * @author chenxueli
 * @date 2020/9/22 9:23
 */
public class UpdateIncreaseOrder extends AbstractMethod {

    public UpdateIncreaseOrder() {
        super("updateIncreaseOrder");
    }

    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        var sqlMethod = SqlMethod.UPDATE_INCREASE_ORDER;
        var sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), "set sort_order = sort_order + 1", SqlScriptUtil.sqlWhereEntityWrapper(true, tableInfo, "sort_order >= #{sort}"), this.sqlComment());
        var sqlSource = this.languageDriver.createSqlSource(this.configuration, sql, modelClass);
        return this.addUpdateMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource);
    }
}
